<?php

/*
 * Originally developed by Jerome, fixed for WP2.x by Inaki Ecenarro
 */

require_once 'classes/LightPressPlugin.php';

class AdminOptions extends LightPressPlugin {

    var $constructor_args = array();
    var $default_context = LP_CONTEXT_ALL;
    var $description = 'Add links to the WP console for login/logout, editing posts and comments';

    var $active = true;
    var $hooks = array('parse_post', 'parse_comment', 'post_render');

    var $_frontend;
	var $_userdata = array();
    
    var $_show_edit_links;
    var $_user;
    var $_post_author = '';     // stores author in between 'parse_post' and 'parse_comment' hooks

    function AdminOptions(&$frontend, $args, $dummy_run=false) {
        parent::LightPressPlugin($frontend, $args, $dummy_run);
    }

    function hide() {
       $this->_frontend->tpl->setVar('PLUGIN_ADMINOPTIONS', '');
	   $this->_frontend->tpl->setVar('PLUGIN_ADMINOPTIONS_EDITPOST', '');
       $this->_frontend->tpl->setVar('PLUGIN_ADMINOPTIONS_EDITCOMMENT', '');
    }

    function run($hook, &$post) {
        
		if (! $this->active)
			return;

        $tpl =& $this->_frontend->tpl;
		$context =& $this->_frontend->context;
        
        if (is_null($this->_user)) {
            $this->_user = $this->_frontend->userFromWpCookie();
            if (!$this->_user) {
                // not logged in, show login template, blank out edit templates and disable plugin
                $tpl->setFile('adminoptions_login', 'plugins/adminoptions_login.xml');
                $tpl->parse('PLUGIN_ADMINOPTIONS', 'adminoptions_login');
                $tpl->setVar('PLUGIN_ADMINOPTIONS_EDITPOST', '');
                $tpl->setVar('PLUGIN_ADMINOPTIONS_EDITCOMMENT', '');
                $this->active = false;
                return;
            }
            if ($this->_user['user_level'] > 7)
                $this->_show_edit_links = true;
            else if (isset($this->_user['capabilities']['administrator']))
                $this->_show_edit_links = true;
            else if (isset($this->_user['capabilities']['editor']))
                $this->_show_edit_links = true;
            else
                $this->_show_edit_links = false;
        }
        switch ($hook) {
            case 'parse_post':  // handle post edit links
                if ($this->_frontend->local_context & (LP_CONTEXT_POST | LP_CONTEXT_STATIC)) {
                    // save post author for comments hook later
                    $this->_post_author = $post['post_author_login'];

                    // check if user can edit this post
                    if ($this->_show_edit_links || (($this->_user['user_level'] > 1) && ($this->_user['user_login'] == $post['post_author_login']))) {
                        // edit post link
                        $tpl->setFile('adminoptions_editpost', 'plugins/adminoptions_editpost.xml');
                        $tpl->setVar('post_id', $post['post_id']);
                        $tpl->parse('PLUGIN_ADMINOPTIONS_EDITPOST', 'adminoptions_editpost');
                        return;
                    }
                }
                
                // hide template for this post
                $tpl->setVar('PLUGIN_ADMINOPTIONS_EDITPOST', '');
                break;

            case 'parse_comment':   // handle comment edit links
                if ($this->_frontend->local_context & (LP_CONTEXT_POST | LP_CONTEXT_STATIC)) {
                    // check if user can edit this comment (WordPress allows any user that can edit the post to edit its comments)
                    if ($this->_show_edit_links || ($this->_post_author && ($this->_user['user_level'] > 1) && ($this->_user['user_login'] == $this->_post_author))) {
                        // edit comment link
                        $tpl->setFile('adminoptions_editcomment', 'plugins/adminoptions_editcomment.xml');
                        $tpl->setVar('comment_ID', $post['comment_ID']);
                        $tpl->parse('PLUGIN_ADMINOPTIONS_EDITCOMMENT', 'adminoptions_editcomment');
                        return;
                    }
                }
                // hide template for this comment
                $tpl->setVar('PLUGIN_ADMINOPTIONS_EDITCOMMENT', '');
                break;

            case 'post_render':
                // login/logout/admin block
                $tpl->setFile('adminoptions_login', 'plugins/adminoptions_admin.xml');
                $tpl->parse('PLUGIN_ADMINOPTIONS', 'adminoptions_login');
                break;
		}
	}

}

?>
